package com.heima.a07_demo.server.service.impl;

import com.heima.a07_demo.common.result.Result;
import com.heima.a07_demo.pojo.dto.PatientDTO;

import com.heima.a07_demo.pojo.entity.DiagnosisRecord;
import com.heima.a07_demo.pojo.entity.Doctor;
import com.heima.a07_demo.pojo.entity.Patient;
import com.heima.a07_demo.server.mapper.PatientMapper;
import com.heima.a07_demo.server.service.PatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PatientServiceImp implements PatientService {

    @Autowired
    private PatientMapper patientMapper;

    /**
     * 添加患者
     * @param patient
     * @return
     */
    @Override
    public Long addPatient(Patient patient) {
        int rows = patientMapper.insertPatient(patient);
        if (rows > 0) {
            return patient.getPatientId();
        }
        return null;


    }

    /**
     * 历史诊断记录查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Map<String, Object> getPageHistory(Integer page, Integer pageSize, String name) {
        int offset = (page - 1) * pageSize;
        List<DiagnosisRecord> records = patientMapper.selectPageHistory(name, offset, pageSize);
        int total = patientMapper.countPageHistory(name);

        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("records", records);
        return data;
    }



    /**
     * 分页查询患者诊断记录，不返回页码和每页记录数，只返回总记录数和记录列表
     *
     * @param page     当前页码
     * @param pageSize 每页记录数
     * @return 包含总记录数和记录列表的 Map
     */
    @Override
    public Map<String, Object> getPageNow(Integer page, Integer pageSize) {
        int offset = (page - 1) * pageSize;
        List<DiagnosisRecord> records = patientMapper.selectPageNow(offset, pageSize);
        int total = patientMapper.countPageNow();

        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("records", records);
        return data;
    }

    @Override
    public boolean deleteDiagnosisRecord(Long id) {
        int rows = patientMapper.deleteById(id);
        return rows > 0;
    }

    @Override
    public void updatePatient(PatientDTO dto) {
        Patient patient = new Patient();
        BeanUtils.copyProperties(dto, patient);

        // 执行更新操作
        patientMapper.updatePatientById(patient);

    }

    @Override
    public Patient getById(Long id) {
        Patient patient =patientMapper.getById(id);
        return patient;
    }

    @Override
    public List<Integer> getByIds() {
        List<Integer> ids = patientMapper.getByIds();
        return ids;
    }
}



